﻿using Dapper;
using System.Threading.Tasks;
using Loong.EntityFrameworkCore;

namespace Loong.BackgroundJobs
{
    public class BackgroundJobDbAccessor : EfCoreDbAccessor
    {
        public BackgroundJobDbAccessor(IDbContextProvider<LoongDbContext> dbContextProvider)
            : base(dbContextProvider)
        { }

        public async Task DeleteAsync(long jobId)
        {
            string sql = "DELETE FROM BackgroundJob WHERE `Id`=@Id";

            await Connection.ExecuteAsync(sql, new { Id = jobId }, Transaction);
        }

        public async Task<int> UpdateAsync(BackgroundJobInfo entity)
        {
            string sql = @"
UPDATE BackgroundJob SET
`IsLocked` = @IsLocked,
`LockEndTime` = @LockEndTime,
`LastTryTime` = @LastTryTime,
`NextTryTime` = @NextTryTime,
`TryCount` = @TryCount,
`IsAbandoned` = @IsAbandoned
WHERE `Id` = @Id;
";
            return await Connection.ExecuteAsync(sql, entity, Transaction);
        }
    }
}
